
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

import java.util.Random;

/**
 * Class that calculates combat outcomes based on inputted variables of monster
 * and player.
 */
public class Combat {

    /**
     * Random object used for combat randomization.
     */
    private Random rand = new Random();
    /**
     * Stored variable for the current dungeon.
     */
    private Dungeon dg;
    /**
     * Random chance for the hero damage.
     */
    private final int heroRandom = 5;
    /**
     * Random chance for hero hitting.
     */
    private final int heroHit = 2;
    /**
     * Random chance for monster hitting.
     */
    private final int monsterHit = 3;
    /**
     * Random chance for monster damage.
     */
    private final int monsterRandom = 10;
    /**
     * Hitting chance.
     */
    private final int hitMiss = 20;

    /**
     * Constructor for the Combat class Takes the current dungeon to access
     * methods.
     *
     * @param dgx Current Dungeon.
     */
    public Combat(final Dungeon dgx) {
        dg = dgx;
    }

    /**
     * Attack method that performs player attacks. Uses randomized hit chance
     * and display a message if a miss.
     *
     * @param c Creature attacked
     * @param h Hero Attacking
     * @param l Level used.
     */
    public final void attack(final Creature c, final Hero h, final Level l) {
        boolean killed;
        Sound player = new Sound();
        if (hitOrMiss() > 2) {
            c.setHp(c.getHp() - calculateHeroForce(c, h));
            String temp = String.valueOf(calculateHeroForce(c, h));
            dg.getGui().addEventCombat(c, h, temp, 1);
            if (c.getHp() < 0) {
                h.setGold(h.getGold() + c.getGold());
                h.setXp(h.getXp() + c.getXp());
                l.addLevelObject(c.getArrayX(), c.getArrayY(), new Floor());
                killed = true;
                dg.getGui().addEventCombat(c, h, temp, 4);
                if (killed && c.getClass() == Boss.class) {
                    dg.nextLevel(h);
                    player.playmid("sm3-clear.mid"); //används för att spela upp filen
                } else if (killed && c.getClass() == Skeleton.class) {

                    player.playwav("skeleton.wav"); //används för att spela upp filen
                } else if (killed && c.getClass() == Ghost.class) {

                    player.playwav("ghost1.wav"); //används för att spela upp filen
                } else if (killed == true && c.getClass() == Dragon.class) {

                    player.playwav("drake.wav"); //används för att spela upp filen
                }
            } else {
                dg.getGui().addEventCombat(c, h, " ", 3);
            }
        }
        if (h.getXp() > h.getXpL()) {
            h.levelUp();
            dg.getGui().levelUp();
        }
    }

    /**
     * Attack Method for the creature. Uses randomized hit checking.
     *
     * @param c Creature attacking
     * @param h Hero damaging.
     */
    public final void counterAttack(final Creature c, final Hero h) {
        if (hitOrMiss() > 4) {
            h.setHp(h.getHp() - calculateCreatureForce(c, h));
            String temp = String.valueOf(calculateCreatureForce(c, h));
            dg.getGui().addEventCombat(c, h, temp, 2);
        } else {
            dg.getGui().addEventCombat(c, h, " ", 5);
        }
        if (h.getHp() <= 0) {
            dg.lose(h);
        }
    }

    /**
     * Calculates hero damage.
     *
     * @param c Creature used.
     * @param h Hero Used.
     * @return Hero Damage.
     */
    private int calculateHeroForce(final Creature c, final Hero h) {
        int res;
        int variableforce = rand.nextInt(100);
        res = h.getAttackDmg() + variableforce;
        res = res / c.getArmor();
        return res;
    }

    /**
     * Calculated monster damage. uses randomized range of damage.
     *
     * @param c Creature used.
     * @param h Hero used.
     * @return Creature damage.
     */
    private int calculateCreatureForce(final Creature c, final Hero h) {
        int res;
        int variableforce = rand.nextInt(100);
        res = c.getAttackDmg() + variableforce;
        res = res / h.getArmor();
        return res;
    }

    /**
     * Returns a value used for checking if a hit occurs.
     *
     * @return random number.
     */
    private int hitOrMiss() {
        return rand.nextInt(hitMiss);
    }

    /**
     * Returns a value used for checking if a hit was strong or weak.
     *
     * @return boolean 50/50
     */
    private boolean weakOrStrong() {
        return rand.nextBoolean();
    }
}
